package cn.iocoder.yudao.module.picturebook.controller.admin.classmember;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.picturebook.controller.admin.classmember.vo.*;
import cn.iocoder.yudao.module.picturebook.dal.dataobject.classmember.ClassMemberDO;
import cn.iocoder.yudao.module.picturebook.service.classmember.ClassMemberService;

@Tag(name = "绘本系统 - 班级成员关联")
@RestController
@RequestMapping("/picturebook/class-member")
@Validated
public class ClassMemberController {

    @Resource
    private ClassMemberService classMemberService;

    @PostMapping("/create")
    @Operation(summary = "创建班级成员关联")
    @PreAuthorize("@ss.hasPermission('picturebook:class-member:create')")
    public CommonResult<Long> createClassMember(@Valid @RequestBody ClassMemberSaveReqVO createReqVO) {
        return success(classMemberService.createClassMember(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新班级成员关联")
    @PreAuthorize("@ss.hasPermission('picturebook:class-member:update')")
    public CommonResult<Boolean> updateClassMember(@Valid @RequestBody ClassMemberSaveReqVO updateReqVO) {
        classMemberService.updateClassMember(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除班级成员关联")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('picturebook:class-member:delete')")
    public CommonResult<Boolean> deleteClassMember(@RequestParam("id") Long id) {
        classMemberService.deleteClassMember(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除班级成员关联")
                @PreAuthorize("@ss.hasPermission('picturebook:class-member:delete')")
    public CommonResult<Boolean> deleteClassMemberList(@RequestParam("ids") List<Long> ids) {
        classMemberService.deleteClassMemberListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得班级成员关联")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('picturebook:class-member:query')")
    public CommonResult<ClassMemberRespVO> getClassMember(@RequestParam("id") Long id) {
        ClassMemberDO classMember = classMemberService.getClassMember(id);
        return success(BeanUtils.toBean(classMember, ClassMemberRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得班级成员关联分页")
    @PreAuthorize("@ss.hasPermission('picturebook:class-member:query')")
    public CommonResult<PageResult<ClassMemberRespVO>> getClassMemberPage(@Valid ClassMemberPageReqVO pageReqVO) {
        PageResult<ClassMemberDO> pageResult = classMemberService.getClassMemberPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ClassMemberRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出班级成员关联 Excel")
    @PreAuthorize("@ss.hasPermission('picturebook:class-member:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportClassMemberExcel(@Valid ClassMemberPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ClassMemberDO> list = classMemberService.getClassMemberPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "班级成员关联.xls", "数据", ClassMemberRespVO.class,
                        BeanUtils.toBean(list, ClassMemberRespVO.class));
    }
    //根据班级的邀请码加入班级
    @PostMapping("/join-class")
    @Operation(summary = "加入班级")
    public CommonResult<Boolean> joinClass(@RequestParam("classCode") String classCode,
                                           @RequestParam("memberName") String memberName,
                                           @RequestHeader("token") Long userId) {
        return success(classMemberService.joinClass(classCode,memberName,userId));
    }
    //退出班级
    @PostMapping("/quit-class")
    @Operation(summary = "退出班级")
    public CommonResult<Boolean> quitClass(@RequestParam("classId") Long classId,
                                           @RequestHeader("token") Long userId) {
        return success(classMemberService.quitClass(classId,userId));
    }
    //获取同一个班级的的成员
    @GetMapping("/get-class-allmember")
    @Operation(summary = "获取同一个班级的的成员")
    public CommonResult<List<ClassMemberDataRespVO>> getClassAllMember(@RequestParam("classId") Long classId) {
       List<ClassMemberDataRespVO> classMemberDOList= classMemberService.getClassDataByClassId(classId);
       return success(classMemberDOList);
    }

}